package com.stu.ssm_02.interceptor;

import com.stu.ssm_02.entity.User;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * ClassName: AuthInterceptor
 * Package: com.stu.ssm_02.interceptor
 * Description: @Version 1.0
 */
public class AuthInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("loginUser");
        if(user==null){
            response.sendRedirect("/login/goToLogin");
//            终止后续请求
            return false;
        }
//        放行
        return super.preHandle(request, response, handler);
    }
}
